package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.TMMcAgentProfitSharingInfo;
import cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo;
import cc.rengu.igas.momp.common.entity.TMPlatBizTmp;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.CommonMessageCheck;
import cc.rengu.igas.momp.facade.request.ManageAgentProfitSharingMcRequest;
import cc.rengu.igas.momp.facade.request.ManageAgentProfitSharingMcRequest;
import cc.rengu.igas.momp.facade.response.ManageAgentProfitSharingMcResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 代理商分润类类活动信息管理功能实现
 *
 * @author Jinan Liu
 * @date 2020/4/2 17:45
 */
public class ManageAgentProfitSharingMcService extends RadpService {

    private String globalInstId;
    private String globalMcNo;
    private String globalTimeStamp;
    private String globalOprId;
    private String globalTxnDate;
    private String globalTableName = TableNameEnum.T_M_MC_APS_INFO.getTableNameCode();

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest = new ManageAgentProfitSharingMcRequest();
            ConvertUtil.convertOutput(manageAgentProfitSharingMcRequest);

            ManageAgentProfitSharingMcResponse manageAgentProfitSharingMcResponse = new ManageAgentProfitSharingMcResponse();
            manageAgentProfitSharingMcResponse.setHeader(manageAgentProfitSharingMcRequest.getHeader());
            BizResponse<ManageAgentProfitSharingMcResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(manageAgentProfitSharingMcResponse);

            /* 判断查询类型 */
            rglog.debug("TXN_NUM=<{}>", manageAgentProfitSharingMcRequest.getHeader().getTxnNum());

            String queryType = manageAgentProfitSharingMcRequest.getQueryType();
            String queryRole = manageAgentProfitSharingMcRequest.getQueryRole();
            String oprType = manageAgentProfitSharingMcRequest.getOprType();
            globalOprId = manageAgentProfitSharingMcRequest.getQueryOrgId();
            globalInstId = manageAgentProfitSharingMcRequest.getHeader().getInstId();
            globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalTxnDate = manageAgentProfitSharingMcRequest.getHeader().getTransDate();
            if (!StringUtil.isNullorEmpty(manageAgentProfitSharingMcRequest.getMcNo())) {
                globalMcNo = manageAgentProfitSharingMcRequest.getMcNo();
            }

            rglog.debug("QUERY_TYPE=<{}>", queryType);
            rglog.debug("QUERY_ROLE=<{}>", queryRole);
            rglog.debug("OPR_TYPE=<{}>", oprType);

            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(queryType, globalOprId, queryRole, rglog);
            CommonMessageCheck.checkSingleValue(oprType, rglog);

            // 服务调用
            BizResponse<ManageAgentProfitSharingMcResponse> bizResponseNew = manageAgentProfitSharingMc(
                    manageAgentProfitSharingMcRequest, manageAgentProfitSharingMcResponse, xmlTreeUtil);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 代理商分润类类活动管理
     *
     * @param manageAgentProfitSharingMcRequest  代理商分润类类营销活动管理接口请求对象
     * @param manageAgentProfitSharingMcResponse 代理商分润类类营销活动管理接口应答对象
     * @param xmlTreeUtil                        内部XML树
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.ManageAgentProfitSharingMcResponse> 代理商分润类类营销活动管理接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/29 01:38
     */
    private BizResponse<ManageAgentProfitSharingMcResponse> manageAgentProfitSharingMc(ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest, ManageAgentProfitSharingMcResponse manageAgentProfitSharingMcResponse, XmlTreeUtil xmlTreeUtil)
            throws Exception {

        BizResponse<ManageAgentProfitSharingMcResponse> bizResponse = new BizResponse<>();

        /* 判断查询类型 */
        String queryType = manageAgentProfitSharingMcRequest.getQueryType();
        String queryRole = manageAgentProfitSharingMcRequest.getQueryRole();
        String oprType = manageAgentProfitSharingMcRequest.getOprType();

        /* 判断用户的角色和权限 */
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(queryType)) {
            if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                /* 银行审批岗-正式表和临时表所有数据 */
                if (OprTypeEnum.AUDIT_PASSED.getOprTypeCode().equals(oprType)) {
                    /* 审批通过 */
                    auditPassed(manageAgentProfitSharingMcRequest);

                } else if (OprTypeEnum.AUDIT_REFUSED.getOprTypeCode().equals(oprType)) {
                    /* 审批拒绝 */
                    auditRefused(manageAgentProfitSharingMcRequest);

                } else {
                    rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                    throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
                }

            } else if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                if (OprTypeEnum.AUDIT_DELETE.getOprTypeCode().equals(oprType)) {
                    /* 删除草稿 */
                    deleteDraftMcData(manageAgentProfitSharingMcRequest);
                }
            } else {
                /* 银行录入岗或银行发布岗 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
            }
        } else {
            /* 商户、代理商、收银员或用户 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
        }


        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        manageAgentProfitSharingMcResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        manageAgentProfitSharingMcResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(manageAgentProfitSharingMcResponse);

        return bizResponse;
    }

    /**
     * 删除草稿
     *
     * @param manageAgentProfitSharingMcRequest 代理商分润类类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void deleteDraftMcData(ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageAgentProfitSharingMcRequest.getHeader().getInstId();
        String mcNo = manageAgentProfitSharingMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 查询营销活动信息 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcAgentProfitSharingInfo mcAgentProfitSharingInfo = mcDetailInfoMapper.selectAgentProfitSharingMcInfo(instId, mcNo);

        if (mcAgentProfitSharingInfo == null) {
            /* 获取营销活动信息失败 */
            rglog.error("获取营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
        }

        if (!McStatusEnum.DRAFT.getMcStatusCode().equals(mcAgentProfitSharingInfo.getMcStatus())) {
            /* 营销活动状态异常，不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), mcAgentProfitSharingInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }

        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(mcAgentProfitSharingInfo.getAuditStatus()) ||
                AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(mcAgentProfitSharingInfo.getAuditStatus()) ||
                AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode().equals(mcAgentProfitSharingInfo.getAuditStatus())) {
            /* 审批中不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), mcAgentProfitSharingInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 删除营销活动信息 */
        returnCode = mcDetailInfoMapper.deleteAgentProfitSharingMcInfo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动机构信息 */
        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        returnCode = mcOrgInfoMapper.deleteMcOrgInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动规则信息 */
        McRuleInfoMapper mcRuleInfoMapper = new McRuleInfoMapperImpl();
        returnCode = mcRuleInfoMapper.deleteAgentProfitSharingRuleInfoByPrimaryKey(instId, mcNo, null);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc());
        }

        /*删除临时表信息*/
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        returnCode = platBizTmpMapper.deleteTmpMcDataByMcNo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 删除临时表数据失败 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("删除临时表中营销活动各信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
            throw new BizException(RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespDesc());
        }

        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批通过
     *
     * @param manageAgentProfitSharingMcRequest 代理商分润类类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditPassed(ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageAgentProfitSharingMcRequest.getHeader().getInstId();
        String mcNo = manageAgentProfitSharingMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        rglog.debug("INST_ID=<{}>, MC_NO=<{}>", instId, mcNo);

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcAgentProfitSharingInfo tMMcAgentProfitSharingInfo = (TMMcAgentProfitSharingInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());
        if (null == tMMcAgentProfitSharingInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
        //查询代理商参与的活动
        List<TMMcAgentProfitSharingInfo> tmMcAgentProfitSharingInfoList = mcDetailInfoMapper.selectApsMcInfoList(globalInstId, tMMcAgentProfitSharingInfo.getAgentMchntNo(), globalTxnDate);
        //如果list不为空或条数大于0，抛出
        if (null != tmMcAgentProfitSharingInfoList && tmMcAgentProfitSharingInfoList.size() > 0) {
            rglog.info("该代理商已参加进行中的代理商分润活动，不允许审核活动");
            throw new BizException(RespCodeEnum.WRONG_AGENT_ISALREADY_ORTHER_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_AGENT_ISALREADY_ORTHER_INMC_ERROR.getRespDesc());
        }
        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tMMcAgentProfitSharingInfo.getMcNo());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        String bizStatus = platBizTmp.getBizStatus();
        if (BizStatusEnum.TMP_STATUS.getBizStatusCode().equals(bizStatus)) {
            /* 非正式数据要更新为正式数据 */
            bizStatus = BizStatusEnum.NORMAL_STATUS.getBizStatusCode();
        }

        /* 根据现有营销活动状态判断如更新营销活动状态 */
        String mcStatus;
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(platBizTmp.getMcStatus())) {
            /* 当前营销活动状态是草稿状态,需要更新营销活动状态为未发布 */
            mcStatus = McStatusEnum.NOT_PUBLISHED.getMcStatusCode();
        } else {
            /* 当前营销活动状态不是草稿状态,不需要更新营销活动状态 */
            mcStatus = platBizTmp.getMcStatus();
        }

        /* 根据现有审批状态决定如何更新临时表数据 */
        String oldAuditStatus;
        String newAuditStatus;
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setBizStatus(bizStatus);
        tmPlatBizTmp.setMcStatus(mcStatus);
        tmPlatBizTmp.setAuditStatus(newAuditStatus);
        /* 更新临时表审批状态 */
        returnCode = platBizTmpMapper.updateTmpMcInfoByMcNoAndAuditStatus(instId, mcNo, tmPlatBizTmp, oldAuditStatus, globalOprId, globalTimeStamp);
//        /* 更新临时表数据 */
//        returnCode = platBizTmpMapper.updateTmpMcAllStatusByMcNoAndAuditStatus(instId, mcNo, bizStatus, mcStatus, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp);
        if (Database.DBS_SUCCESS != returnCode) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据,营销活动状态是草稿或未发布状态才需要操作正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tMMcAgentProfitSharingInfo.getMcStatus()) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tMMcAgentProfitSharingInfo.getMcStatus())) {

            if (McStatusEnum.DRAFT.getMcStatusCode().equals(tMMcAgentProfitSharingInfo.getMcStatus())) {
                /* 如果营销活动状态是草稿状态,需要将草稿状态变更为未发布状态,同时将审批状态变更为发布-审批流程中 */
                TMMcAgentProfitSharingInfo tMMcAgentProfitSharingInfo1 = new TMMcAgentProfitSharingInfo();
                tMMcAgentProfitSharingInfo1.setInstId(instId);
                tMMcAgentProfitSharingInfo1.setMcNo(mcNo);
                tMMcAgentProfitSharingInfo1.setMcStatus(McStatusEnum.NOT_PUBLISHED.getMcStatusCode());
                tMMcAgentProfitSharingInfo1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
                tMMcAgentProfitSharingInfo1.setLastOprId(globalOprId);
                tMMcAgentProfitSharingInfo1.setUpdateTime(globalTimeStamp);
                tMMcAgentProfitSharingInfo1.setAuditRefuseReason(manageAgentProfitSharingMcRequest.getOprContent());

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tMMcAgentProfitSharingInfo1, globalTableName);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            } else {
                /* 如果营销活动状态是未发布状态,需要将审批状态变更为发布-审批流程中 */
                if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tMMcAgentProfitSharingInfo.getAuditStatus()) ||
                        AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tMMcAgentProfitSharingInfo.getAuditStatus())) {

                    newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

                } else {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
                }

                /* 更新正式表审批状态 */
                TMMcAgentProfitSharingInfo tmMcAgentProfitSharingInfo1 = new TMMcAgentProfitSharingInfo();
                tmMcAgentProfitSharingInfo1.setInstId(globalInstId);
                tmMcAgentProfitSharingInfo1.setMcNo(globalMcNo);
                tmMcAgentProfitSharingInfo1.setMcStatus(mcStatus);
                tmMcAgentProfitSharingInfo1.setAuditStatus(newAuditStatus);
                tmMcAgentProfitSharingInfo1.setLastOprId(globalOprId);
                tmMcAgentProfitSharingInfo1.setUpdateTime(globalTimeStamp);

                returnCode = mcDetailInfoMapper.updateMcInfoByMcNoAndAuditStatusAndTableName(tmMcAgentProfitSharingInfo1, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());

//                /* 更新正式表数据 */
//                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_APS_INFO.getTableNameCode(), instId, mcNo, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            }
        } else {
            /* 营销活动状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }

        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批拒绝
     *
     * @param manageAgentProfitSharingMcRequest 代理商分润类类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditRefused(ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageAgentProfitSharingMcRequest.getHeader().getInstId();
        String mcNo = manageAgentProfitSharingMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcAgentProfitSharingInfo tMMcAgentProfitSharingInfo = (TMMcAgentProfitSharingInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());
        if (null == tMMcAgentProfitSharingInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tMMcAgentProfitSharingInfo.getMcNo());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 判断如何更新临时表数据 */
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
//            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
//                    AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
            TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
            tmPlatBizTmp.setBizStatus(platBizTmp.getBizStatus());
            tmPlatBizTmp.setMcStatus(platBizTmp.getMcStatus());
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
            tmPlatBizTmp.setAuditRefuseReason(manageAgentProfitSharingMcRequest.getOprContent());
            /* 更新临时表审批状态 */
            returnCode = platBizTmpMapper.updateTmpMcInfoByMcNoAndAuditStatus(instId, mcNo, tmPlatBizTmp,
                    AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(), globalOprId, globalTimeStamp);

            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批拒绝 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
//            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
//                    AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());

            TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
            tmPlatBizTmp.setBizStatus(platBizTmp.getBizStatus());
            tmPlatBizTmp.setMcStatus(platBizTmp.getMcStatus());
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
            tmPlatBizTmp.setAuditRefuseReason(manageAgentProfitSharingMcRequest.getOprContent());
            /* 更新临时表审批状态 */
            returnCode = platBizTmpMapper.updateTmpMcInfoByMcNoAndAuditStatus(instId, mcNo, tmPlatBizTmp,
                    AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(), globalOprId, globalTimeStamp);

            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批拒绝 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else {
            /* 营销活动临时数据审批状态异常，不可审批拒绝 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tMMcAgentProfitSharingInfo.getMcStatus()) ||
                McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tMMcAgentProfitSharingInfo.getMcStatus())) {

            /* 更新正式表审批状态 */
            TMMcAgentProfitSharingInfo tmMcAgentProfitSharingInfo1 = new TMMcAgentProfitSharingInfo();
            tmMcAgentProfitSharingInfo1.setInstId(instId);
            tmMcAgentProfitSharingInfo1.setMcNo(mcNo);
            tmMcAgentProfitSharingInfo1.setMcStatus(tMMcAgentProfitSharingInfo.getMcStatus());
            tmMcAgentProfitSharingInfo1.setAuditRefuseReason(manageAgentProfitSharingMcRequest.getOprContent());
            tmMcAgentProfitSharingInfo1.setLastOprId(globalOprId);
            tmMcAgentProfitSharingInfo1.setUpdateTime(globalTimeStamp);
            tmMcAgentProfitSharingInfo1.setPublishRefuseReason(tMMcAgentProfitSharingInfo.getPublishRefuseReason());

            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tMMcAgentProfitSharingInfo.getAuditStatus())) {
//                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_APS_INFO.getTableNameCode(), instId, mcNo,
//                        AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(),
//                        globalOprId, globalTimeStamp);
                //设置审批状态为 新增——审批拒绝
                tmMcAgentProfitSharingInfo1.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
                returnCode = mcDetailInfoMapper.updateMcInfoByMcNoAndAuditStatusAndTableName(tmMcAgentProfitSharingInfo1, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());

                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批拒绝 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tMMcAgentProfitSharingInfo.getAuditStatus())) {
//                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_APS_INFO.getTableNameCode(), instId, mcNo,
//                        AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(),
//                        globalOprId, globalTimeStamp);
                //设置审批状态为 修改——审批拒绝
                tmMcAgentProfitSharingInfo1.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
                returnCode = mcDetailInfoMapper.updateMcInfoByMcNoAndAuditStatusAndTableName(tmMcAgentProfitSharingInfo1, TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());

                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批拒绝 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else {
                /* 营销活动审批状态异常，不可审批拒绝 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
            }

        } else {
            /* 营销活动审批状态异常，不可审批拒绝 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }


        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }
}
